<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="货主" prop="ownerId">
        <el-select v-model="queryParams.ownerId" placeholder="请选择货主" clearable>
          <el-option
            v-for="owner in cargoOwnerList"
            :key="owner.id"
            :label="owner.ownerName"
            :value="owner.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="仓库" prop="warehouseId">
        <el-select v-model="queryParams.warehouseId" placeholder="请选择仓库" clearable>
          <el-option
            v-for="warehouse in warehouseList"
            :key="warehouse.id"
            :label="warehouse.warehouseName"
            :value="warehouse.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="库位" prop="locationId">
        <el-select v-model="queryParams.locationId" placeholder="请选择库位" clearable>
          <el-option
            v-for="location in locationList"
            :key="location.id"
            :label="location.locationCode"
            :value="location.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="容器号" prop="containerNo">
        <el-input
          v-model="queryParams.containerNo"
          placeholder="请输入容器号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品编码" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入商品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次号" prop="batchNo">
        <el-input
          v-model="queryParams.batchNo"
          placeholder="请输入批次号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择库存状态" clearable>
          <el-option
            v-for="dict in dict.type.wms_inventory_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wms:inventory:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wms:inventory:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:inventory:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-lock"
          size="mini"
          :disabled="multiple"
          @click="handleFreeze"
          v-hasPermi="['wms:inventory:freeze']"
        >冻结</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-unlock"
          size="mini"
          :disabled="multiple"
          @click="handleUnfreeze"
          v-hasPermi="['wms:inventory:unfreeze']"
        >解冻</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wms:inventory:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inventoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="货主名称" align="center" prop="ownerName" />
      <el-table-column label="仓库名称" align="center" prop="warehouseName" />
      <el-table-column label="商品编码" align="center" prop="itemCode" show-overflow-tooltip />
      <el-table-column label="商品名称" align="center" prop="itemName" show-overflow-tooltip />
      <el-table-column label="供应商名称" align="center" prop="supplierName" show-overflow-tooltip />
      <el-table-column label="库位编码" align="center" prop="locationCode" />
      <el-table-column label="容器号" align="center" prop="containerNo" show-overflow-tooltip />
      <el-table-column label="批次号" align="center" prop="batchNo" />
      <el-table-column label="可用数量" align="center" prop="availableQty">
        <template slot-scope="scope">
          <span :class="{'text-danger': scope.row.availableQty <= 0}">{{ scope.row.availableQty }}</span>
        </template>
      </el-table-column>
      <el-table-column label="冻结数量" align="center" prop="frozenQty">
        <template slot-scope="scope">
          <span :class="{'text-warning': scope.row.frozenQty > 0}">{{ scope.row.frozenQty }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总数量" align="center" prop="totalQty" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="生产日期" align="center" prop="productionDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.productionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="过期日期" align="center" prop="expirationDate" width="100">
        <template slot-scope="scope">
          <span :class="{'text-danger': isExpiringSoon(scope.row.expirationDate)}">{{ parseTime(scope.row.expirationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="库存状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_inventory_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            v-hasPermi="['wms:inventory:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:inventory:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-lock"
            @click="handleFreeze(scope.row)"
            v-hasPermi="['wms:inventory:freeze']"
            v-if="scope.row.availableQty > 0"
          >冻结</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-unlock"
            @click="handleUnfreeze(scope.row)"
            v-hasPermi="['wms:inventory:unfreeze']"
            v-if="scope.row.frozenQty > 0"
          >解冻</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wms:inventory:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库存信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="货主" prop="ownerId">
              <el-select v-model="form.ownerId" placeholder="请选择货主" style="width: 100%" @change="handleOwnerChange">
                <el-option
                  v-for="owner in cargoOwnerList"
                  :key="owner.id"
                  :label="owner.ownerName"
                  :value="owner.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓库" prop="warehouseId">
              <el-select v-model="form.warehouseId" placeholder="请选择仓库" style="width: 100%" @change="handleWarehouseChange">
                <el-option
                  v-for="warehouse in warehouseList"
                  :key="warehouse.id"
                  :label="warehouse.warehouseName"
                  :value="warehouse.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="库位" prop="locationId">
              <el-select v-model="form.locationId" placeholder="请选择库位" style="width: 100%" @change="handleLocationChange">
                <el-option
                  v-for="location in locationList"
                  :key="location.id"
                  :label="location.locationCode"
                  :value="location.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="容器号" prop="containerNo">
              <el-input v-model="form.containerNo" placeholder="请输入容器号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商" prop="supplierId">
              <el-select v-model="form.supplierId" placeholder="请选择供应商" style="width: 100%" @change="handleSupplierChange">
                <el-option
                  v-for="supplier in supplierList"
                  :key="supplier.id"
                  :label="supplier.supplierName"
                  :value="supplier.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>

          <el-col :span="12">
            />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="批次号" prop="batchNo">
              <el-input v-model="form.batchNo" placeholder="请输入批次号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="unit">
              <el-input v-model="form.unit" placeholder="请输入单位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="可用数量" prop="availableQty">
              <el-input-number v-model="form.availableQty" :min="0" :precision="2" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="冻结数量" prop="frozenQty">
              <el-input-number v-model="form.frozenQty" :min="0" :precision="2" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="总数量" prop="totalQty">
              <el-input-number v-model="form.totalQty" :min="0" :precision="2" style="width: 100%" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="生产日期" prop="productionDate">
              <el-date-picker
                v-model="form.productionDate"
                type="date"
                placeholder="选择生产日期"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                style="width: 100%"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="过期日期" prop="expirationDate">
              <el-date-picker
                v-model="form.expirationDate"
                type="date"
                placeholder="选择过期日期"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                style="width: 100%"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="库存状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择库存状态" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.wms_inventory_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 冻结库存对话框 -->
    <el-dialog title="冻结库存" :visible.sync="freezeOpen" width="500px" append-to-body>
      <el-form ref="freezeForm" :model="freezeForm" :rules="freezeRules" label-width="120px">
        <el-form-item label="商品信息">
          
        </el-form-item>
        <el-form-item label="可用数量">
          <el-input :value="freezeForm.availableQty" disabled />
        </el-form-item>
        <el-form-item label="冻结数量" prop="freezeQty">
          <el-input-number v-model="freezeForm.freezeQty" :min="0" :max="freezeForm.availableQty" :precision="2" style="width: 100%" />
        </el-form-item>
        <el-form-item label="冻结原因" prop="reason">
          <el-input v-model="freezeForm.reason" type="textarea" placeholder="请输入冻结原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFreeze">确 定</el-button>
        <el-button @click="freezeOpen = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 解冻库存对话框 -->
    <el-dialog title="解冻库存" :visible.sync="unfreezeOpen" width="500px" append-to-body>
      <el-form ref="unfreezeForm" :model="unfreezeForm" :rules="unfreezeRules" label-width="120px">
        <el-form-item label="商品信息">
          
        </el-form-item>
        <el-form-item label="冻结数量">
          <el-input :value="unfreezeForm.frozenQty" disabled />
        </el-form-item>
        <el-form-item label="解冻数量" prop="unfreezeQty">
          <el-input-number v-model="unfreezeForm.unfreezeQty" :min="0" :max="unfreezeForm.frozenQty" :precision="2" style="width: 100%" />
        </el-form-item>
        <el-form-item label="解冻原因" prop="reason">
          <el-input v-model="unfreezeForm.reason" type="textarea" placeholder="请输入解冻原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitUnfreeze">确 定</el-button>
        <el-button @click="unfreezeOpen = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 库存详情对话框 -->
    <el-dialog title="库存详情" :visible.sync="detailOpen" width="800px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="货主名称">{{ detailForm.ownerName }}</el-descriptions-item>
        <el-descriptions-item label="仓库名称">{{ detailForm.warehouseName }}</el-descriptions-item>
        <el-descriptions-item label="库位编码">{{ detailForm.locationCode }}</el-descriptions-item>
        <el-descriptions-item label="容器号">{{ detailForm.containerNo }}</el-descriptions-item>
        <el-descriptions-item label="供应商名称">{{ detailForm.supplierName }}</el-descriptions-item>
        <el-descriptions-item label="商品编码">{{ detailForm.itemCode }}</el-descriptions-item>
        <el-descriptions-item label="商品名称">{{ detailForm.itemName }}</el-descriptions-item>
        <el-descriptions-item label="批次号">{{ detailForm.batchNo }}</el-descriptions-item>
        <el-descriptions-item label="单位">{{ detailForm.unit }}</el-descriptions-item>
        <el-descriptions-item label="可用数量">{{ detailForm.availableQty }}</el-descriptions-item>
        <el-descriptions-item label="冻结数量">{{ detailForm.frozenQty }}</el-descriptions-item>
        <el-descriptions-item label="总数量">{{ detailForm.totalQty }}</el-descriptions-item>
        <el-descriptions-item label="库存状态">
          <dict-tag :options="dict.type.wms_inventory_status" :value="detailForm.status"/>
        </el-descriptions-item>
        <el-descriptions-item label="生产日期">{{ parseTime(detailForm.productionDate, '{y}-{m}-{d}') }}</el-descriptions-item>
        <el-descriptions-item label="过期日期">{{ parseTime(detailForm.expirationDate, '{y}-{m}-{d}') }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ parseTime(detailForm.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</el-descriptions-item>
        <el-descriptions-item label="更新时间">{{ parseTime(detailForm.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ detailForm.remark }}</el-descriptions-item>
      </el-descriptions>
      
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInventory, getInventory, delInventory, addInventory, updateInventory, freezeInventory, unfreezeInventory } from "@/api/wms/inventory";
import { listCargoOwner } from "@/api/wms/cargoOwner";
import { listWarehouse } from "@/api/wms/warehouse";
import { listLocation } from "@/api/wms/location";
import { listSupplier } from "@/api/wms/supplier";

export default {
  name: "Inventory",
  dicts: ['wms_inventory_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存信息表格数据
      inventoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示冻结对话框
      freezeOpen: false,
      // 是否显示解冻对话框
      unfreezeOpen: false,
      // 是否显示详情对话框
      detailOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ownerId: null,
        warehouseId: null,
        locationId: null,
        containerNo: null,
        itemCode: null,
        itemName: null,
        batchNo: null,
        status: null
      },
      // 表单参数
      form: {},
      // 冻结表单
      freezeForm: {},
      // 解冻表单
      unfreezeForm: {},
      // 详情表单
      detailForm: {},
      // 表单校验
      rules: {
        ownerId: [
          { required: true, message: "货主不能为空", trigger: "change" }
        ],
        warehouseId: [
          { required: true, message: "仓库不能为空", trigger: "change" }
        ],
        locationId: [
          { required: true, message: "库位不能为空", trigger: "change" }
        ],


        availableQty: [
          { required: true, message: "可用数量不能为空", trigger: "blur" }
        ],
        unit: [
          { required: true, message: "单位不能为空", trigger: "blur" }
        ]
      },
      // 冻结表单校验
      freezeRules: {
        freezeQty: [
          { required: true, message: "冻结数量不能为空", trigger: "blur" }
        ],
        reason: [
          { required: true, message: "冻结原因不能为空", trigger: "blur" }
        ]
      },
      // 解冻表单校验
      unfreezeRules: {
        unfreezeQty: [
          { required: true, message: "解冻数量不能为空", trigger: "blur" }
        ],
        reason: [
          { required: true, message: "解冻原因不能为空", trigger: "blur" }
        ]
      },
      // 货主列表
      cargoOwnerList: [],
      // 仓库列表
      warehouseList: [],
      // 库位列表
      locationList: [],
      // 供应商列表
      supplierList: []
    };
  },
  watch: {
    // 监听可用数量和冻结数量变化，自动计算总数量
    'form.availableQty'() {
      this.calculateTotalQty();
    },
    'form.frozenQty'() {
      this.calculateTotalQty();
    }
  },
  created() {
    this.getList();
    this.getCargoOwnerList();
    this.getWarehouseList();
    this.getLocationList();
    this.getSupplierList();
  },
  methods: {
    /** 查询库存信息列表 */
    getList() {
      this.loading = true;
      listInventory(this.queryParams).then(response => {
        this.inventoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询货主列表 */
    getCargoOwnerList() {
      listCargoOwner({ status: '0' }).then(response => {
        this.cargoOwnerList = response.rows;
      });
    },
    /** 查询仓库列表 */
    getWarehouseList() {
      listWarehouse({ status: '0' }).then(response => {
        this.warehouseList = response.rows;
      });
    },
    /** 查询库位列表 */
    getLocationList() {
      listLocation({ status: '0' }).then(response => {
        this.locationList = response.rows;
      });
    },
    /** 查询供应商列表 */
    getSupplierList() {
      listSupplier({ status: '0' }).then(response => {
        this.supplierList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        inventoryId: null,
        ownerId: null,
        ownerName: null,
        supplierId: null,
        supplierName: null,
        warehouseId: null,
        warehouseName: null,
        locationId: null,
        locationCode: null,
        containerNo: null,
        itemCode: null,
        itemName: null,
        batchNo: null,
        productionDate: null,
        expirationDate: null,
        availableQty: 0,
        frozenQty: 0,
        totalQty: 0,
        unit: null,
        status: "NORMAL",
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.inventoryId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库存信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const inventoryId = row.inventoryId || this.ids
      getInventory(inventoryId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库存信息";
      });
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      getInventory(row.inventoryId).then(response => {
        this.detailForm = response.data;
        this.detailOpen = true;
      });
    },
    /** 冻结按钮操作 */
    handleFreeze(row) {
      if (row && row.inventoryId) {
        // 单个冻结
        this.freezeForm = {
          inventoryId: row.inventoryId,
  
          availableQty: row.availableQty,
          freezeQty: 0,
          reason: ''
        };
      } else {
        // 批量冻结
        if (this.ids.length === 0) {
          this.$modal.msgError("请选择要冻结的库存");
          return;
        }
        this.freezeForm = {
          inventoryIds: this.ids,
          freezeQty: 0,
          reason: ''
        };
      }
      this.freezeOpen = true;
    },
    /** 解冻按钮操作 */
    handleUnfreeze(row) {
      if (row && row.inventoryId) {
        // 单个解冻
        this.unfreezeForm = {
          inventoryId: row.inventoryId,
  
          frozenQty: row.frozenQty,
          unfreezeQty: 0,
          reason: ''
        };
      } else {
        // 批量解冻
        if (this.ids.length === 0) {
          this.$modal.msgError("请选择要解冻的库存");
          return;
        }
        this.unfreezeForm = {
          inventoryIds: this.ids,
          unfreezeQty: 0,
          reason: ''
        };
      }
      this.unfreezeOpen = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.inventoryId != null) {
            updateInventory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInventory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 冻结提交 */
    submitFreeze() {
      this.$refs["freezeForm"].validate(valid => {
        if (valid) {
          freezeInventory(this.freezeForm).then(response => {
            this.$modal.msgSuccess("冻结成功");
            this.freezeOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 解冻提交 */
    submitUnfreeze() {
      this.$refs["unfreezeForm"].validate(valid => {
        if (valid) {
          unfreezeInventory(this.unfreezeForm).then(response => {
            this.$modal.msgSuccess("解冻成功");
            this.unfreezeOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const inventoryIds = row.inventoryId || this.ids;
      this.$modal.confirm('是否确认删除库存信息编号为"' + inventoryIds + '"的数据项？').then(function() {
        return delInventory(inventoryIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/inventory/export', {
        ...this.queryParams
      }, `inventory_${new Date().getTime()}.xlsx`)
    },
    /** 货主变更处理 */
    handleOwnerChange(ownerId) {
      const owner = this.cargoOwnerList.find(item => item.id === ownerId);
      if (owner) {
        this.form.ownerName = owner.ownerName;
      }
    },
    /** 仓库变更处理 */
    handleWarehouseChange(warehouseId) {
      const warehouse = this.warehouseList.find(item => item.id === warehouseId);
      if (warehouse) {
        this.form.warehouseName = warehouse.warehouseName;
      }
    },
    /** 库位变更处理 */
    handleLocationChange(locationId) {
      const location = this.locationList.find(item => item.id === locationId);
      if (location) {
        this.form.locationCode = location.locationCode;
      }
    },
    /** 供应商变更处理 */
    handleSupplierChange(supplierId) {
      const supplier = this.supplierList.find(item => item.id === supplierId);
      if (supplier) {
        this.form.supplierName = supplier.supplierName;
      }
    },
    /** 计算总数量 */
    calculateTotalQty() {
      this.form.totalQty = (this.form.availableQty || 0) + (this.form.frozenQty || 0);
    },
    /** 判断是否即将过期 */
    isExpiringSoon(expirationDate) {
      if (!expirationDate) return false;
      const today = new Date();
      const expDate = new Date(expirationDate);
      const diffTime = expDate - today;
      const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
      return diffDays <= 30; // 30天内过期显示红色
    }
  }
};
</script>

<style scoped>
.text-danger {
  color: #f56c6c;
}
.text-warning {
  color: #e6a23c;
}
</style>